package org.hero.reptile;

import cn.hutool.core.collection.CollUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class BiqugerReptile implements Reptile{

    /* 小说目录URL */
    private static final String NOVEL_URL = "http://www.biquger.net/html/0/752/index.html";


    /* 章节URL前缀 */
    private static final String CHAPTER_URL_PREFIX = "http://www.biquger.net";

    private static final String NAME_CLASS = "lb_fm";

    public void run() throws Exception {
        Document document = Jsoup.connect(NOVEL_URL).get();
        Novel novel = new Novel();

        setNovelName(novel, document);
        setNovelAuthor(novel, document);

        setChapterList(novel, document);
    }

    public void setChapterList(Novel novel, Document document) {


        Element list = document.getElementById("chapter_outsite");
        Elements onechapter = list.getElementsByClass("onechapter");

    }

    public void setNovelAuthor(Novel novel, Document document) {
        Elements elementsByClass = document.getElementsByClass(NAME_CLASS);
        String author = elementsByClass.get(0)
                .getElementsByTag("table").get(0)
                .getElementsByTag("tbody").get(0)
                .getElementsByTag("tr").get(0)
                .getElementsByTag("td").get(0)
                .getElementsByTag("div").get(1).text();
        novel.setNovelAuthor(author);
    }

    public void setNovelName(Novel novel, Document document) {
        Elements h1s = document.getElementsByTag("h1");
        if (CollUtil.isNotEmpty(h1s)) {
            String name = h1s.get(0)
                    .text()
                    .replaceAll(" ", "")
                    .replaceAll("章节列表", "");
            novel.setNovelName(name);
        }
    }
}
